/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * SuiteBase.java
 *
 * Created on November 24, 2005, 9:42 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package ptd_suite_objects;

import java.io.*;
import java.util.Vector;
import java.nio.ByteBuffer;
import java.lang.Integer;
import ptd.Main;
import ptd.PTDDisplay;
import ptd.PTDHTMLPane;
import ptd.PTDRemoteMachine;
import ptd.PTDScriptComm;
import ptd.PTDScriptRunner;
import ptd.PTDSuiteDisplay;

/**
 *
 * @author John
 */
public class SuiteBase extends Object{
    
    public String myName;
    public String myDescription;
    public String myPreprocessor;
    public String myPostprocessor;
    public int myID;
    public int myType;
    public int myChildrenCount;
    public int myRunSelectState;
    protected boolean bModified;
    
    private int mySepChar;
    
    public static int SUITE_TYPE = 1;
    public static int TESTCASE_TYPE = 2;
    public static int SCRIPT_TYPE = 3;
    public static int PREPROC_TYPE = 4;
    public static int POSTPROC_TYPE = 5;
    
    
    // We dont need children, since it will be handled in TreeView
    //private Vector myChildren;
    
    /** Creates a new instance of SuiteBase */
    public SuiteBase() {
        myChildrenCount = 0;
        myID = 0;
        myRunSelectState = 1;
        myType = SUITE_TYPE;
        myName = "Item";
        myDescription = "";
        myPreprocessor = "";
        myPostprocessor = "";
        mySepChar = 29;
        bModified = false;
    }

    public SuiteBase(SuiteBase Other) {
        myChildrenCount = Other.myChildrenCount;
        myID = Other.myID;
        myRunSelectState = Other.myRunSelectState;
        myType = Other.myType;
        myName = Other.myName;
        myDescription = Other.myDescription;
        myPreprocessor = Other.myPreprocessor;
        myPostprocessor = Other.myPostprocessor;
        mySepChar = Other.mySepChar;
        bModified = false;
    }

    
    
    // --- OVERRIDEABLES -----
    public boolean isEqualTo(SuiteBase Other){
        if(myName.compareTo(Other.myName) != 0){
            return false;
        }
        if(myDescription.compareTo(Other.myDescription) != 0){
            return false;
        }
        if(myPreprocessor.compareTo(Other.myPreprocessor) != 0){
            return false;
        }
        if(myPostprocessor.compareTo(Other.myPostprocessor) != 0){
            return false;
        }        
        if(myID != Other.myID){
            return false;
        }
        
        if(myType != Other.myType){
            return false;
        }
        
        if(myChildrenCount != Other.myChildrenCount){
            return false;
        }

        if(myRunSelectState != Other.myRunSelectState){
            return false;
        }
        
        return true;
    }
    
    public boolean getModified(){
        boolean bRet = bModified;
        bModified = false;
        return bRet;
    }

    
    protected void setModified(){
        bModified = true;
    }

    
    protected int do_load(String s){
        int iPos = s.indexOf(mySepChar);
        if(iPos < 0){
            return 0;
        }
        return 1;
    }
    
    protected int do_load(FileInputStream f){
        Integer anInt = new Integer(0);
        anInt = loadInt(f);
        if(anInt == null) return 0;
        myChildrenCount = anInt.intValue();
        anInt = loadInt(f);
        if(anInt == null) return 0;
        this.myRunSelectState = anInt.intValue();
        
        String s;
        // Get Name
        s = loadString(f);
        if(s == null) return 0;
        myName = s;

        // Get Description
        s = loadString(f);
        if(s == null) return 0;
        myDescription = s;

        s = loadString(f);
        if(s == null) return 0;
        myPreprocessor = s;

        s = loadString(f);
        if(s == null) return 0;
        myPostprocessor = s;

        
        return 1;
    }

    protected int do_save(String s){
        
        return 1;
    }
    
    protected int do_save(FileOutputStream f){

        Integer anInt = new Integer(myType);
        if(saveInt(f, anInt) == false) return 0;
            
        anInt = new Integer(myChildrenCount);
        if(saveInt(f, anInt) == false) return 0;

        anInt = new Integer(myRunSelectState);
        if(saveInt(f, anInt) == false) return 0;
        
        if(saveString(f, myName) == false) return 0;
            
        if(saveString(f, myDescription) == false) return 0;
        
        if(saveString(f, myPreprocessor) == false) return 0;
        
        if(saveString(f, myPostprocessor) == false) return 0;
            
        return 1;
    }

    
    
    // --- LOCALS -----
    public int load(String s){
        // Just load base objects first
        return do_load(s);
    }
    
    public int load(FileInputStream f){
        // Just load base objects first
        return do_load(f);
    }

    public int save(String s){
        // Just load base objects first
        bModified = false;
        return do_save(s);
    }
    
    public int save(FileOutputStream f){
        // Just load base objects first
        bModified = false;
        return do_save(f);
    }

    public static int loadType(FileInputStream f){
        try{
            ByteBuffer b = ByteBuffer.allocate(4);
            byte[] bb = new byte[4];
            f.read(bb);
            b.put(bb);
            b.flip();
            int iRet = b.getInt();
            return iRet;
        }catch(Exception e){
            Main.debug_out(e.getMessage());
            return  0;
        }
    }
    
    public static boolean saveString(FileOutputStream f, String In){
        try{
            ByteBuffer b = ByteBuffer.allocate(4);
            byte[] bb;
            b.clear();
            b.putInt(In.length());
            bb = b.array();
            f.write(bb);
            f.write(In.getBytes());
            return true;
        }catch (Exception e){
            Main.debug_out(e.getMessage());
            return false;
        }
    }

    public static boolean saveInt(FileOutputStream f, Integer In){
        try{
            ByteBuffer b = ByteBuffer.allocate(4);
            b.putInt(In.intValue());
            byte[] bb = b.array();
            f.write(bb);
            return true;
        }catch (Exception e){
            Main.debug_out(e.getMessage());
            return false;
        }
    }
    
    
    public static String loadString(FileInputStream f){
        try{
            ByteBuffer b = ByteBuffer.allocate(4);
            byte[] bb = new byte[4];
            f.read(bb);
            b.clear();
            b.put(bb);
            b.flip();
            int iLen = b.getInt();
            bb = new byte[iLen];
            f.read(bb);
            String Ret = new String(bb);
            return Ret;
        }catch(Exception e){
            Main.debug_out(e.getMessage());
            return null;
        }
    }
    
    public static Integer loadInt(FileInputStream f){
        try{
            ByteBuffer b = ByteBuffer.allocate(4);
            byte[] bb = new byte[4];
            f.read(bb);
            b.put(bb);
            b.flip();
            Integer iRet = new Integer(b.getInt());
            return iRet;
        }catch(Exception e){
            Main.debug_out(e.getMessage());
            return  null;
        }
    }
    
    public void initRun(PTDDisplay inDisp){
        inDisp.startSuite(myName);
    }

    
    public int run(PTDDisplay inDisp, PTDScriptComm inComm){
        return 1;
    }

    public int runRemote(PTDDisplay inDisp, PTDScriptComm inComm, PTDRemoteMachine Machine){
        return 1;
    }
    
    public int runPreprocessor(PTDDisplay inDisp, PTDScriptComm inComm){
        
        if(myPreprocessor.length() > 0){
            inDisp.startProcessor(SuiteBase.PREPROC_TYPE, myPreprocessor);
            PTDScriptRunner aRunner = new PTDScriptRunner();
            
            if(aRunner.loadFile(myPreprocessor) == 0){
                inDisp.displayError("Preprocess file [" + myPreprocessor + "] does not exist.\n");
            }else{
                aRunner.setOutput(inDisp);
                aRunner.setComm(inComm);        
                aRunner.run(true, false, true);
            }
            inDisp.stopProcessor();
        }
        
        return 1;
    }

    public int runPostprocessor(PTDDisplay inDisp, PTDScriptComm inComm){
        
        if(myPostprocessor.length() > 0){            
            inDisp.startProcessor(SuiteBase.POSTPROC_TYPE, myPostprocessor);
            PTDScriptRunner aRunner = new PTDScriptRunner();
            
            if(aRunner.loadFile(myPostprocessor) == 0){
                inDisp.displayError("Postprocess file [" + myPostprocessor + "] does not exist.\n");
            }else{
                aRunner.setOutput(inDisp);
                aRunner.setComm(inComm);        
                aRunner.run(true, false, true);
            }
            inDisp.stopProcessor();
        }
        return 1;
    }

    
    
    public SuiteBase duplicate(){
        return new SuiteBase(this);
        /*
        SuiteBase Ret = new SuiteBase();
        Ret.myChildrenCount = myChildrenCount;
        Ret.myDescription = myDescription;
        Ret.myPreprocessor = myPreprocessor;
        Ret.myPostprocessor = myPostprocessor;

        Ret.myID = myID;
        Ret.myName = myName;
        Ret.myRunSelectState = myRunSelectState;
        Ret.mySepChar = mySepChar;
        Ret.myType = myType;
        return Ret;
         **/
    }
    
}
